<?php
namespace app\shop\controller\api;

use app\shop\library\ApiAuthController;
use app\shop\library\Member;
class Address extends ApiAuthController{
    public function onSave(){
        $data['detail'] = $this->post('detail',$this->check()->type('string')->required());
        $data['phone'] = $this->post('phone',$this->check()->type('string')->reg('/^1[3456789]\d{9}$/')->required());
        $data['contacts'] = $this->post('contacts',$this->check()->type('string')->required());
        $data['sex'] = $this->post('sex',$this->check()->type('int')->required());
        $data['tag'] = $this->post('tag',$this->check()->type('string')->required());

        $address_id = $this->post('address_id',$this->check()->type('int'));
        
        $address = $this->model('MemberAddress');

        if ($address_id){

            $oldData = $address->getId($address_id);

            if ($oldData['member_id'] != Member::$id){
                throw new \Exception('非法操作');
            }

            if ($address->updateId($address_id,$data)){
                Member::clearCache('address');
                return ['address_id' => $address_id];
            }
        }else{
            
            if ($address->where('member_id',Member::$id)->count()>=5){
                throw new \Exception('最多只能添加5个地址');
            }

            $data['member_id'] = Member::$id;
            if ($address_id = $address->insert($data)){
                Member::clearCache('address');
                return ['address_id' => $address_id];
            }
        }

        throw new \Exception('保存失败');
    }

    public function onList(){
        return Member::address();
    }
    public function onDetail(){
        $address_id = $this->post('id',$this->check()->type('int')->required());

        $address = $this->model('MemberAddress');

        $data = $address->getId($address_id);

        if ($data['member_id'] != Member::$id){
            throw new \Exception('非法操作');
        }

        return $data;
    }

    public function onDel(){
        $address_id = $this->post('id',$this->check()->type('int')->required());

        $address = $this->model('MemberAddress');

        $data = $address->getId($address_id);

        if ($data['member_id'] != Member::$id){
            throw new \Exception('非法操作');
        }

        //如果就剩一个地址，则不能删除
        $count = $address->where('member_id',Member::$id)->count();

        if ($count <= 1){
            throw new \Exception('至少保留一个地址');
        }

        if ($address->deleteId($address_id)){

            Member::clearCache('address');
            return true;
        }

        throw new \Exception('删除失败');
    }

    public function onDefault(){
        $address_id = $this->post('address_id',$this->check()->type('int')->required());

        $address = $this->model('MemberAddress');

        $data = $address->getId($address_id);

        if ($data['member_id'] != Member::$id){
            throw new \Exception('非法操作');
        }

        $address->where('member_id',Member::$id)->update(['default'=>0]);
        $address->updateId($address_id,['default'=>1]);

        Member::clearCache('address');
        return true;
    }
}